﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using zhangmenren.Models.help;

namespace zhangmenren.Untils
{
	public class UpLoadHelper
	{
		//图片上传接口
		public static ResultImg UploadFile(string url, string param)
		{
			ResultImg result = null;
			if (!string.IsNullOrEmpty(url))
			{
				string data = Http.RequestPost(url, param);
				if (!string.IsNullOrEmpty(data))
				{
					var obj = JsonConvert.DeserializeObject<JObject>(data);
					if (obj != null && (bool)obj["succeed"] == true)
					{
						result = new ResultImg() { code = 1, msg = "上传成功", url = obj["result"]["url"].ToString(), name = obj["result"]["name"].ToString() };
					}
					else
						result = new ResultImg() { code = 0, msg = "上传失败", url = "", name = "" };
				}
			}
			else
				result = new ResultImg() { code = -1, msg = "参数不能为空", url = "", name = "" };
			return result;
		}

		/// <summary>
		/// 流转二进制数组
		/// </summary>
		public static byte[] StreamToBytes(Stream stream)
		{
			byte[] bytes = new byte[stream.Length];
			stream.Read(bytes, 0, bytes.Length);
			stream.Seek(0, SeekOrigin.Begin);
			return bytes;
		}

		//获取图片格式
		public static string GetExtension(string filename)
		{
			if (String.IsNullOrEmpty(filename)) return "jpg";
			var extension = Path.GetExtension(filename).Replace(".", "");
			return extension;
		}






	}
}
